﻿Imports System.Data.OleDb
Imports LecturerAppointmentSystem.UserDetails

Public Class AdministratorHome
    Inherits System.Web.UI.Page

    Protected Sub Page_PreRenderer(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        createLetterList()
        UserListGridView.DataSource = Membership.GetAllUsers()
        UserListGridView.DataBind()
    End Sub

    Protected Sub lb_UserList_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_UserList.Click
        AdminMultiView.SetActiveView(UserListView)
    End Sub

    Sub createLetterList()
        Dim lettersArray() As String = {"All", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
        Dim buttonArray(26) As Button
        Dim y As Integer = 0
        Dim index As Integer = 0
        For Each letter In lettersArray
            buttonArray(index) = New Button()
            AlphabetPanel.Controls.AddAt(y, buttonArray(index))
            buttonArray(index).Text = letter.ToString()
            AddHandler buttonArray(index).Click, AddressOf setNameDataSource 'add event handler function.
            buttonArray(index).Attributes.Add("runat", "server")
            buttonArray(index).Attributes.Add("OnClick", "setNameDataSource")
            y = y + 1
            index = index + 1
        Next
        AlphabetPanel.Visible = False
    End Sub

    Function getRoleList()
        Dim roleArray As ArrayList = New ArrayList
        Dim sql As String = "SELECT * FROM aspnet_Roles"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.HasRows() Then
            While reader.Read()
                roleArray.Add(reader("RoleName"))
            End While
            conn.Close()
        End If
        Return roleArray
    End Function

    Sub setNameDataSource(ByVal sender As Object, ByVal e As EventArgs)
        Dim btn As Button = CType(sender, Button)
        Dim letter As String = btn.Text
        If letter = "All" Then
            UserListGridView.DataSource = Membership.GetAllUsers()
        Else
            UserListGridView.DataSource = Membership.FindUsersByName(letter & "%")
        End If
        UserListGridView.DataBind()
    End Sub

    'Sub setRoleDataSource(ByVal sender As Object, ByVal e As EventArgs)
    '    Dim btn As Button = CType(sender, Button)
    '    Dim roleName As String = btn.Text

    'End Sub

    Protected Sub lb_UsersByName_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_UsersByName.Click
        RolePanel.Visible = False
        AlphabetPanel.Visible = True
    End Sub

    Protected Sub lb_UsersByRoles_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_UsersByRoles.Click
        AlphabetPanel.Visible = False
        RolePanel.Visible = True
        ddl_RoleList.DataSource = getRoleList()
        ddl_RoleList.Visible = True
        ddl_RoleList.DataBind()
    End Sub

    Protected Sub setRoleDataSource()
        Dim allUsers As MembershipUserCollection = Membership.GetAllUsers()
        Dim filteredUsers As New MembershipUserCollection()

        If ddl_RoleList.SelectedIndex > 0 Then
            ' If we are filtering by role, get the users in the specified role

            Dim usersInRole() As String = Roles.Provider.GetUsersInRole(ddl_RoleList.SelectedValue)

            ' For each user in the role, add the user details to filteredUsers
            For Each user As MembershipUser In allUsers
                For Each userInRole As String In usersInRole
                    If userInRole = user.UserName Then
                        filteredUsers.Add(user)
                        Exit For ' Breaks out of the inner foreach loop to avoid unneeded checking.
                    End If
                Next userInRole
            Next user
        Else
            ' We are not filtering by role...
            filteredUsers = allUsers
        End If

        ' Bind the users to the Users GridView
        UserListGridView.DataSource = filteredUsers
        UserListGridView.DataBind()
    End Sub
    Protected Sub AddBlankItem(ByVal sender As Object, ByVal e As EventArgs)
        ddl_RoleList.Items.Insert(0, New ListItem("Any", "0"))
    End Sub
End Class